package com.zmn.oms.dubbo.impl.zmn.factory.part;

import org.apache.dubbo.config.annotation.Service;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.dubbo.dto.zmn.part.fcpart.*;
import com.zmn.oms.dubbo.interfaces.zmn.factory.part.FactoryOrderPartRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.factory.OrderFactory;
import com.zmn.oms.model.entity.fcorderpart.*;
import com.zmn.oms.model.entity.fcorderpart.detail.FcOrderPartDetail;
import com.zmn.oms.model.entity.fcorderpart.detail.FcOrderPartPostDetail;
import com.zmn.oms.services.interfaces.factory.OrderFactoryService;
import com.zmn.oms.services.interfaces.fcorderpart.OrderFactoryPartPostService;
import com.zmn.oms.services.interfaces.fcorderpart.OrderFactoryPartService;
import com.zmn.oms.zmn.business.interfaces.part.ZsOrderFactoryPartBService;
import com.zmn.oms.zmn.normal.business.interfaces.order.ZsNormalOrderBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：
 *
 * @author LiangHailong
 * @date 2019/05/15 15:42
 */
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class FactoryOrderPartRemoteServiceImpl implements FactoryOrderPartRemoteService {

    private Logger logger = LoggerFactory.getLogger(FactoryOrderPartRemoteServiceImpl.class);
    private static final String TAG = "啄木鸟-厂商订单配件";

    @Autowired
    ZsOrderFactoryPartBService orderFactoryPartBService;
    @Autowired
    OrderFactoryPartPostService orderFactoryPartPostService;
    @Autowired
    ZsNormalOrderBService zsNormalOrderBService;
    @Autowired
    OrderFactoryPartService orderFactoryPartService;
    @Autowired
    OrderFactoryService orderFactoryService;

    /**
     *  通过orderId 查询配件
     *
     */
    @Override
    public ResponseDTO<FcOrderPartDRO> findPartByOrderId(Long orderId){
        try {

            List<OrderFactoryPart> parts = orderFactoryPartService.listOrderFactoryPartByOrderId(orderId);
            List<FcOrderPartDRO> fcOrderPartDROS = BeanMapper.mapList(parts, FcOrderPartDRO.class);
            return ResponseUtil.responseSuccess(fcOrderPartDROS,"OK");

        } catch(Exception e){
            e.printStackTrace();
            logger.error("[{}] 查询错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }

    }

    /**
     * 根据订单id查询最新批次的配件,用于厂商配件审核
     * @param orderId
     * @return
     */
    @Override
    public ResponseDTO<FcOrderPartDRO> partsReview(Long orderId){
        try {
            List<OrderFactoryPart> orderFactoryParts = orderFactoryPartBService.findParts4Review(orderId);
            return ResponseUtil.responseSuccess(BeanMapper.mapList(orderFactoryParts,FcOrderPartDRO.class),"OK");

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[{}] 查询错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }
    }
    /**
     * 保存配件审核结果
     * @param fcOrderPartReviewBatchDIO
     * @return
     */
    @Override
    public ResponseDTO saveReview(FcOrderPartReviewBatchDIO fcOrderPartReviewBatchDIO) {
        try {
            FcOrderPartReview fcOrderPartReview = BeanMapper.map(fcOrderPartReviewBatchDIO, FcOrderPartReview.class);
            fcOrderPartReview.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_FACTORY);
            // 保存配件审核
            orderFactoryPartBService.savePartsReview(fcOrderPartReview);
            return ResponseUtil.responseSuccess();
        } catch (Exception e){
            e.printStackTrace();
            logger.error("[{}] 保存审核错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }
    }

    /**
     * 查询列表
     * @param fcOrderPartQueryDIO
     * @return
     */
    @Override
    public ResponseDTO<FcOrderPartListPageDRO> listPageByQuery(FcOrderPartQueryDIO fcOrderPartQueryDIO) {
        try {
            FcOrderPartQuery fcOrderPartQuery = BeanMapper.map(fcOrderPartQueryDIO, FcOrderPartQuery.class);
            List<OrderFactoryPart> orderFactoryParts = orderFactoryPartService.listPageByQuery(fcOrderPartQuery);
            FcOrderPartListPageDRO fcOrderPartListPageDRO = new FcOrderPartListPageDRO();
            List<FcOrderPartDRO> partList = BeanMapper.mapList(orderFactoryParts,FcOrderPartDRO.class);
            partList.forEach(part -> {
                part.setSrcName(OrderConsts.getFcPartTypeName(part.getSrc()));
            });
            fcOrderPartListPageDRO.setList(partList);
            fcOrderPartListPageDRO.setTotalCount(fcOrderPartQuery.getTotalCount());
            return ResponseUtil.responseSuccess(fcOrderPartListPageDRO,"OK");
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("[{}] 查询列表错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }

    }

    /**
     * 查询配件详情
     * @param fcOrderPartId
     * @return
     */
    @Override
    public ResponseDTO<FcOrderPartDetailDRO> getDetail(Integer fcOrderPartId) {
        try {
            FcOrderPartDetail detail = orderFactoryPartBService.detail(fcOrderPartId);
            // 数据转换
            FcOrderPartDetailDRO detailDRO = BeanMapper.map(detail, FcOrderPartDetailDRO.class);
            detailDRO.setSrcName(OrderConsts.getFcPartTypeName(detailDRO.getSrc()));
            //
            List<FcOrderPartPostDetailDRO> postInfo = detailDRO.getPostInfo();

            postInfo.forEach(e -> {
                e.setStatusName(GlobalConsts.getPostStatusName(e.getStatus()));
                e.setPostTypeName(OrderConsts.getFactoryPartPostTypeName(e.getFcOrderPartPostType()));
            });

            return ResponseUtil.responseSuccess(detailDRO,"ok");
        } catch (Exception e){
            logger.error("[{}] 查询详情错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }

    }

    /**
     * 旧件返厂
     * @param fcOrderOldPartReturnDIO
     * @return
     */
    @Override
    public ResponseDTO saveOldPartReturn(FcOrderOldPartReturnDIO fcOrderOldPartReturnDIO) {
        try {
            FcOrderOldPartReturn orderOldPartReturn = BeanMapper.map(fcOrderOldPartReturnDIO, FcOrderOldPartReturn.class);
            orderOldPartReturn.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_FACTORY);
            orderFactoryPartBService.saveOldPartReturn(orderOldPartReturn);
            return ResponseUtil.responseSuccess();
        } catch (Exception e){
            e.printStackTrace();
            logger.error("[{}] 保存旧件返厂错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }
    }

    /**
     *  邮寄配件
     * @param fcOrderPartPostDIO
     * @return
     */
    @Override
    public ResponseDTO savePartPost(FcOrderPartPostDIO fcOrderPartPostDIO) {
        try {
            FcOrderPartPost fcOrderPartPost = BeanMapper.map(fcOrderPartPostDIO, FcOrderPartPost.class);
            orderFactoryPartBService.savePartPost(fcOrderPartPost);
            return ResponseUtil.responseSuccess();
        } catch (Exception e){
            e.printStackTrace();
            logger.error("[{}] 保存邮寄错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }
    }

    /**
     * 配件撤回
     * @param fcOrderPartId
     * @return
     */
    @Override
    public ResponseDTO saveRecall(Integer fcOrderPartId,Long orderId) {
        try {
            orderFactoryPartBService.recall(fcOrderPartId,orderId);
            return ResponseUtil.responseSuccess();
        } catch (Exception e){
            e.printStackTrace();
            logger.error("[{}] 配件撤回错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }
    }

    @Override
    public ResponseDTO findPostInfo(Integer fcOrderPartId) {
        try {

            List<OrderFactoryPartPost> postList = orderFactoryPartPostService.findByFcOrderPartId(fcOrderPartId);
            return ResponseUtil.responseSuccess(BeanMapper.mapList(postList,FcOrderPartPostDetailDRO.class),"OK");
        } catch (Exception e){
            e.printStackTrace();
            logger.error("[{}] 查询详情错误：{}", TAG, e);
            return ResponseUtil.responseFail(e.getMessage());
        }

    }

    @Override
    public com.zmn.common.dto2.ResponseDTO<List<FcOrderPartPostDetailDRO>> findPostInfoByOrderId(Long orderId) {
        try {
            List<OrderFactoryPartPost> postList = orderFactoryPartPostService.listOrderFactoryPartPostByOrderId(orderId);
            return com.zmn.common.dto2.ResponseDTO.success(BeanMapper.mapList(postList,FcOrderPartPostDetailDRO.class));
        } catch (Exception e){
            e.printStackTrace();
            logger.error("[{}] 查询详情错误：{}", TAG, e);
            return com.zmn.common.dto2.ResponseDTO.fail(e.getMessage());
        }

    }


}
